home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / tests / testinit / testinit.c < prev   
Encoding:
C/C++ Source or Header  |  1992-03-23  |  4.3 KB  |  194 lines

  1. /* 
  2.  * testinit.c --
  3.  *
  4.  *    Test initial user program.
  5.  *
  6.  * Copyright 1991 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that this copyright
  10.  * notice appears in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /user5/kupfer/spriteserver/tests/testinit/RCS/testinit.c,v 1.13 92/03/23 15:10:43 kupfer Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include <cfuncproto.h>
  21. #include <errno.h>
  22. #include <pwd.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys.h>
  27. #include <sys/file.h>
  28. #include <sys/types.h>
  29. #include <sys/wait.h>
  30. #include <test.h>
  31. #include <unistd.h>
  32.  
  33. #define CONSOLE    "/dev/console"    /* name of console device */
  34.  
  35. extern void msleep();
  36. static void SetID _ARGS_((void));
  37.  
  38.  
  39. /*
  40.  *----------------------------------------------------------------------
  41.  *
  42.  * main --
  43.  *
  44.  *    For now, just call a shell.  
  45.  *
  46.  * Results:
  47.  *    None.
  48.  *
  49.  * Side effects:
  50.  *    None.
  51.  *
  52.  *----------------------------------------------------------------------
  53.  */
  54.     
  55. int
  56. main(argc, argv)
  57.     int argc;
  58.     char **argv;
  59. {
  60.     pid_t pid;
  61.     char *shellName = "cmds.sprited/psh";
  62.     int i;
  63.     union wait exitStatus;
  64.  
  65.     Test_PutDecimal(argc);
  66.     Test_PutMessage(" arguments: \n");
  67.     for (i = 0; i < argc; i++) {
  68.     Test_PutMessage(argv[i]);
  69.     Test_PutMessage("\n");
  70.     }
  71.     if (argc >= 2) {
  72.     shellName = argv[1];
  73.     }
  74.  
  75.     /* 
  76.      * Open the console, which will enable use of stdio.  If that fails, 
  77.      * bail out.
  78.      */
  79.     if ((open(CONSOLE, O_RDONLY, 0) != 0)
  80.         || (open(CONSOLE, O_WRONLY, 0) != 1)
  81.         || (open(CONSOLE, O_WRONLY, 0) != 2)) {
  82.     Test_PutMessage("Couldn't open console: ");
  83.     Test_PutMessage(strerror(errno));
  84.     Test_PutMessage(".\n");
  85.     exit(2);
  86.     }
  87.  
  88. /* 
  89.  * Redefine the Test_ calls to use stdio.
  90.  */
  91. #include <testRedef.h>
  92.  
  93.     /* 
  94.      * cd to a test directory, then start up the shell or whatever program 
  95.      * we're suppposed to run.
  96.      */
  97.     if (chdir("/users/kupfer") < 0) {
  98.     Test_PutMessage("Can't chdir to /users/kupfer: ");
  99.     Test_PutMessage(strerror(errno));
  100.     Test_PutMessage("\n");
  101.     goto bailOut;
  102.     }
  103.  
  104.     pid = fork();
  105.     if (pid < 0) {
  106.     Test_PutMessage("Fork failed: ");
  107.     Test_PutMessage(strerror(errno));
  108.     Test_PutMessage("\n");
  109.     goto bailOut;
  110.     } 
  111.     if (pid == 0) {
  112.     char *args[2];
  113.  
  114.     /* Change the user ID to be non-root. */
  115.     SetID();
  116.  
  117.     args[0] = shellName;
  118.     args[1] = 0;
  119. #if 0
  120.     Test_PutMessage("I'm the child (pid ");
  121.     Test_PutHex(getpid());
  122.     Test_PutMessage(")\n");
  123. #endif
  124.     if (execv(shellName, args) < 0) {
  125.         Test_PutMessage("Couldn't exec `");
  126.         Test_PutMessage(shellName);
  127.         Test_PutMessage("': ");
  128.         Test_PutMessage(strerror(errno));
  129.         Test_PutMessage("\n");
  130.         exit(1);
  131.     }
  132.     exit(0);
  133.     } else {
  134. #if 0
  135.     Test_PutMessage("Child is pid ");
  136.     Test_PutHex(pid);
  137.     Test_PutMessage("\n");
  138. #endif
  139.     pid = wait(&exitStatus);
  140.     Test_PutMessage("process ");
  141.     Test_PutHex(pid);
  142.     Test_PutMessage(" finished, code ");
  143.     Test_PutDecimal(exitStatus.w_retcode);
  144.     if (exitStatus.w_termsig != 0) {
  145.         Test_PutMessage(", signal ");
  146.         Test_PutDecimal(exitStatus.w_termsig);
  147.     }
  148.     Test_PutMessage(".\n");
  149.     }
  150.  
  151.  bailOut:
  152.     (void)Sys_Shutdown(SYS_HALT|SYS_KILL_PROCESSES|SYS_WRITE_BACK, NULL);
  153. }
  154.  
  155.  
  156. /*
  157.  *----------------------------------------------------------------------
  158.  *
  159.  * SetID --
  160.  *
  161.  *    Set the user ID of the process to something less potentially 
  162.  *    harmful than root.  
  163.  *
  164.  * Results:
  165.  *    None.
  166.  *
  167.  * Side effects:
  168.  *    None.
  169.  *
  170.  *----------------------------------------------------------------------
  171.  */
  172.  
  173. static void
  174. SetID()
  175. {
  176.     struct passwd *passwdEntPtr; /* password entry for designated user ID */
  177.     char *user = "kupfer";
  178.     uid_t expectedUid = 891;
  179.  
  180.     passwdEntPtr = getpwnam(user);
  181.     if (passwdEntPtr == NULL) {
  182.     fprintf(stderr, "Warning: couldn't find passwd entry for %s.\n",
  183.         user);
  184.     } else if (passwdEntPtr->pw_uid != expectedUid) {
  185.     fprintf(stderr, "Warning: expected uid %d for %s, got %d.\n",
  186.         expectedUid, user, passwdEntPtr->pw_uid);
  187.     }
  188.  
  189.     if (setuid(expectedUid) < 0) {
  190.     fprintf(stderr, "Couldn't change user ID to %d: %s\n",
  191.         expectedUid, strerror(errno));
  192.     }
  193. }
  194.